<template>
  <el-container>
    <!-- header头部区域 -->
    <el-header style="height:100px">
      <div class="home_logo">
        <!-- logo -->
        <img src="../assets/images/heima.png" alt="" />
        <!-- 标题 -->
        <h1>后台电商管理系统</h1>
      </div>
      <!-- 退出登录 -->
      <el-button type="primary" @click="quitLogin">退出</el-button>
    </el-header>
    <el-container>
      <!-- aside菜单栏区域 -->
      <!--     @open="handleOpen" -->
      <!--  @close="handleClose" -->
      <el-aside width="280px">
        <el-menu background-color="#545c64" text-color="#fff" active-text-color="#ffd04b" unique-opened router :default-active="activePath">
          <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="saveNavState('/' + subItem.path)">
              <i class="el-icon-menu"></i>
              <span>{{ subItem.authName }}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- main内容区 -->
      <el-main>
        <!-- 占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'Home',
  data() {
    return {
      menulist: [],
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      activePath: ''
    }
  },
  created() {
    this.getmenuInfo()
    this.activePath = window.sessionStorage.getItem('active')
  },
  methods: {
    // 退出登录
    quitLogin() {
      // 清除token 值
      window.sessionStorage.removeItem('token')
      // 跳转login页面路由
      this.$router.push('/login')
    },
    // 获取菜单栏的列表数据
    async getmenuInfo() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
    },
    //
    saveNavState(activePath) {
      window.sessionStorage.setItem('active', activePath)
    }
  }
}
</script>

<style lang="less" scoped>
.el-container {
  height: 100%;
}
.el-header {
  background-color: #5b5e61;
  // padding: 0 30px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  .home_logo {
    display: flex;
    h1 {
      margin-left: 18px;
      color: #fff;
    }
  }
}
.el-aside {
  background-color: #5b5e61;
}
.el-main {
  background-color: #f5f5dc;
}
.iconfont {
  margin-right: 12px;
  font-size: 20px;
}
</style>
